package com.ddstation.crm.login.service;

import com.ddstation.crm.institution.entity.*;
import com.ddstation.crm.institution.mapper.DtHealthManageInstitutionDoctorMapper;
import com.ddstation.crm.institution.model.UserAccountModel;

import com.ddstation.crm.menu.mapper.DtInvsManageInstitutionMenuMinusMapper;
import com.ddstation.crm.organization_structure.entity.DtInvsOrganizationStructureEntity;
import com.ddstation.crm.organization_structure.mapper.DtInvsOrganizationStructureMapper;
import com.ddstation.crm.organization_structure.mapper.DtInvsOrganizationStructureStaffMapper;
import com.ddstation.crud.entity.DtInvsManageInstitutionSupervisorEntity;
import com.ddstation.crud.entity.DtInvsSupervisorEntity;
import com.ddstation.crud.mapper.DtInvsManageInstitutionSupervisorMapper;
import com.ddstation.crud.mapper.DtInvsSupervisorMapper;
import com.ddstation.doctor.mapper.DdDoctorMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import java.util.*;

@Service
public class LoginAccountServiceImpl implements LoginAccountService {
	
	@Override
	@Transactional(readOnly=true)
	public DtHealthManageInstitutionDoctorEntity get(String loginUser) {
		// TODO Auto-generated method stub
		DtHealthManageInstitutionDoctorEntity c = new DtHealthManageInstitutionDoctorEntity();
		c.setLoginUser(loginUser);
		return dtHealthManageInstitutionDoctorMapper.selectByLoginUser(c);
	}

	@Transactional(rollbackFor=Exception.class)
	public String newAccount(String institutionName, Integer type, String businessLicenceCd, String name, String phone, int accountType, String loginUser, String password) {
		// TODO Auto-generated method stub
		Map<String,Object> map = newAccount(institutionName,type,businessLicenceCd,name,phone,accountType,loginUser,password,null,null);
		return map.get("loginPassword").toString();
	}

	@Transactional(rollbackFor=Exception.class)
	public Map<String,Object> newAccount(String institutionName, Integer type, String businessLicenceCd, String name, String phone, int accountType, String loginUser, String password, String district, String address) {
		// TODO Auto-generated method stub
		return null;
	}

	@Transactional(rollbackFor=Exception.class)
	public int add(String institutionId, String name, String phone, String infoImg,
				   String loginUser, String loginPassword,
				   Integer orgId, Integer isManager,
				   List<Integer> menuIdList) {
		// TODO Auto-generated method stub
		//dt_invs_supervisor 获得 supervisor_id
		DtInvsSupervisorEntity supervisorEntity = new DtInvsSupervisorEntity();
		supervisorEntity.setName(name);
		supervisorEntity.setHeadImgUrl(infoImg);
		supervisorEntity.setPhone(phone);
		dtInvsSupervisorMapper.insertSelective(supervisorEntity);
		int supervisorId = supervisorEntity.getId();
		if(supervisorId > 0){
			//插入数据到 dt_invs_manage_institution_supervisor 登录账户密码
			DtInvsManageInstitutionSupervisorEntity InstitutionSupervisorEntity = new DtInvsManageInstitutionSupervisorEntity();
			InstitutionSupervisorEntity.setSupervisorId(supervisorId);
			InstitutionSupervisorEntity.setInstitutionId(institutionId);
			InstitutionSupervisorEntity.setLoginUser("invs_"+loginUser);
			InstitutionSupervisorEntity.setLoginPassword(loginPassword);
			if(dtInvsManageInstitutionSupervisorMapper.insertSelective(InstitutionSupervisorEntity) > 0){
				//添加部门关系
				String doctorId = "" + supervisorId;//权限相关所有 doctorId 即为 supervisorId
				if(dtInvsOrganizationStructureStaffMapper.insert(orgId, doctorId, isManager) > 0){
					//员工屏蔽权限
					if(menuIdList != null){
						dtInvsManageInstitutionMenuMinusMapper.insert(institutionId,orgId,supervisorId,menuIdList,1);
					}
					return supervisorId;
				}else{
					throw new RuntimeException("添加成员失败！无法创建部门关系");
				}
			}else{
				throw new RuntimeException("添加成员失败！无法创建登录账户");
			}
		}else{
			throw new RuntimeException("添加成员失败！无法创建管理账户");
		}
	}

	@Transactional(rollbackFor=Exception.class)
	public int replace(
			String institutionId, String doctorId, String name, String phone, String infoImg,
			String loginUser, String loginPassword,
			Integer orgId, Integer isManager,
			List<Integer> menuIdList) {
		// TODO Auto-generated method stub
		//更新名称  dt_invs_supervisor
		if (name != null || phone != null || infoImg != null) {
			DtInvsSupervisorEntity entity = new DtInvsSupervisorEntity();
			entity.setId(Integer.parseInt(doctorId));
			entity.setName(name);
			entity.setPhone(phone);
			entity.setHeadImgUrl(infoImg);
			dtInvsSupervisorMapper.updateByPrimaryKeySelective(entity);
		}

		//更新操作者信息 dt_invs_manage_institution_supervisor
		if (loginUser != null || loginPassword != null) {
			DtInvsManageInstitutionSupervisorEntity entity = new DtInvsManageInstitutionSupervisorEntity();
			entity.setInstitutionId(institutionId);
			entity.setSupervisorId(Integer.parseInt(doctorId));
			entity.setLoginUser("invs_"+loginUser);
			entity.setLoginPassword(loginPassword);
			dtInvsManageInstitutionSupervisorMapper.updateByPrimaryKeySelective(entity);
		}

		//更新所在部门信息
		if (orgId != null) {
			this.release(doctorId, institutionId);
			dtInvsOrganizationStructureStaffMapper.insert(orgId, doctorId, isManager);
			if(menuIdList != null){
				//员工屏蔽权限
				dtInvsManageInstitutionMenuMinusMapper.delete(institutionId, Integer.parseInt(doctorId), 1);
				if(menuIdList.size() > 0){
					dtInvsManageInstitutionMenuMinusMapper.insert(institutionId, orgId, Integer.parseInt(doctorId), menuIdList,1);
				}
			}
		}
		return 1;
	}
	
//	@Transactional(rollbackFor=Exception.class)
	private int replaceAccountType(String institutionId, String doctorId, int accountType) {
		// TODO Auto-generated method stub
		DtHealthManageInstitutionBuyAccountEntity buyAccount = dtHealthManageInstitutionDoctorMapper.selectInstitutionBuyAccountByDoctorId(doctorId, institutionId);
		if (buyAccount.getAccounttypeId() == accountType) {
			return 1;
		}
		Integer id = dtHealthManageInstitutionDoctorMapper.selectMinBuyAccount(institutionId, accountType);
		if (id == null) {
			return -1;
		}
		dtHealthManageInstitutionDoctorMapper.removeBuyAccount(doctorId, institutionId);
		return dtHealthManageInstitutionDoctorMapper.updateBuyAccount(id, doctorId);

	}

	@Transactional(rollbackFor=Exception.class)
	public int remove(String institutionId, String doctorId) {
		// TODO Auto-generated method stub
		int supervisorId = Integer.parseInt(doctorId);
		//删除登录表信息 dt_invs_manage_institution_supervisor
		if (dtInvsManageInstitutionSupervisorMapper.deleteByPrimaryKey(supervisorId,institutionId) == 1) {
			//删除监督员表信息 dt_invs_supervisor
			dtInvsSupervisorMapper.deleteByPrimaryKey(supervisorId);
			//删除部门关系表信息 dt_invs_organization_structure_staff
			dtInvsOrganizationStructureStaffMapper.deleteByDoctorId(supervisorId);
			//删除无权限表信息 dt_invs_manage_institution_menu_minus
			dtInvsManageInstitutionMenuMinusMapper.delete(institutionId,supervisorId,1);
			return 1;
		} else {
			throw new RuntimeException("删除员工失败");
		}
	}
	
	public List<Map<String, Object>> getRemainderAccountType(String id) {
		// TODO Auto-generated method stub
		return dtHealthManageInstitutionDoctorMapper.selectAccountType(id);
	}

	public Integer getRemainderAccountCnt(String id) {
		// TODO Auto-generated method stub
		
		return dtHealthManageInstitutionDoctorMapper.selectAccountCnt(id);
	}
	
	public Integer checkUserName(String userName) {
		// TODO Auto-generated method stub
		UserAccountModel existUser = dtHealthManageInstitutionDoctorMapper.selectAccountByLoginUser(userName);
		if (existUser != null) {
			return 1;
		}
		return 0;
	}
	
	public List<UserAccountModel> getAllAccount(String id) {
		// TODO Auto-generated method stub
		return dtHealthManageInstitutionDoctorMapper.selectInstitutionDoctor(id);
	}
	
	private void release(String doctorId, String institutionId) {
		List<DtInvsOrganizationStructureEntity> orgStructs = dtInvsOrganizationStructureMapper.select(institutionId,null);
		List<Integer> orgIdArray = new ArrayList<Integer>();
		for (DtInvsOrganizationStructureEntity orgStruct : orgStructs) {
			orgIdArray.add(orgStruct.getId());
		}
		dtInvsOrganizationStructureStaffMapper.release(doctorId, orgIdArray);
	}
//	
//	private String getRandomPwd() {
//		String pwd = RandomUtil.createRandomChar(6);
//		return pwd.replaceAll("l", "n").replaceAll("I", "J").replaceAll("1", "2").replaceAll("o", "p").replaceAll("O", "P").replaceAll("0", "9");
//	}

//	@Inject private DdDoctorServiceItf doctorService = null;
	@Inject private DdDoctorMapper ddDoctorMapper = null;
//	@Inject private DtHealthManageInstitutionMapper dtHealthManageInstitutionMapper = null;
	@Inject private DtHealthManageInstitutionDoctorMapper dtHealthManageInstitutionDoctorMapper = null;
	@Inject private DtInvsOrganizationStructureStaffMapper dtInvsOrganizationStructureStaffMapper = null;
	@Inject private DtInvsOrganizationStructureMapper dtInvsOrganizationStructureMapper = null;
	@Inject private DtInvsSupervisorMapper dtInvsSupervisorMapper = null;
	@Inject private DtInvsManageInstitutionSupervisorMapper dtInvsManageInstitutionSupervisorMapper = null;
	@Inject private DtInvsManageInstitutionMenuMinusMapper dtInvsManageInstitutionMenuMinusMapper = null;


//	@Inject private DdDoctorHospitalMapper ddDoctorHospitalMapper = null;
//	@Inject private DtHealthManageInstitutionUserhospitalMapper dtHealthManageInstitutionUserhospitalMapper = null;
//	@Inject private DtHospitalCustomerServiceReprersentativeMapper dtHospitalCustomerServiceReprersentativeMapper = null;
//	@Inject private DdDoctorPatientsMapper ddDoctorPatientsMapper = null;
//	@Inject private DdUserDoctorMapper ddUserDoctorMapper = null;
}
